/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
import Brain.CalcuPostfix;
import java.text.DecimalFormat;
/**
 *
 * @author jorgelainfiesta
 */
public class calcupostfix_form extends javax.swing.JFrame {

    private CalcuPostfix calculadora;
    /**
     * Creates new form calcupostfix_form
     */
    public calcupostfix_form() {
        initComponents();
        label_error.setVisible(false);
        label_resultado.setVisible(false);
        label_operacion.setVisible(false);
        //panel_output.setVisible(false);
        this.calculadora = new CalcuPostfix();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_title = new javax.swing.JLabel();
        input_postfix = new javax.swing.JTextField();
        btn_operar = new javax.swing.JButton();
        label_descripcion = new javax.swing.JLabel();
        panel_output = new javax.swing.JPanel();
        label_operacion = new javax.swing.JLabel();
        label_resultado = new javax.swing.JLabel();
        label_error = new javax.swing.JLabel();
        sep_2 = new javax.swing.JSeparator();
        sep_1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(240, 237, 237));

        label_title.setForeground(new java.awt.Color(102, 102, 102));
        label_title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/icons/calculator.png"))); // NOI18N
        label_title.setText("CALCULADORA POSTFIX");

        btn_operar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/icons/calculator-gray.png"))); // NOI18N
        btn_operar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_operarActionPerformed(evt);
            }
        });

        label_descripcion.setFont(new java.awt.Font("Lucida Grande", 2, 12)); // NOI18N
        label_descripcion.setForeground(new java.awt.Color(153, 153, 153));
        label_descripcion.setText("Operadores válidos: +, -, *, /          Acepta Números");

        panel_output.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panel_output.setLayout(new javax.swing.BoxLayout(panel_output, javax.swing.BoxLayout.X_AXIS));

        label_operacion.setFont(new java.awt.Font("Georgia", 2, 13)); // NOI18N
        label_operacion.setForeground(new java.awt.Color(102, 102, 102));
        label_operacion.setText("Operación ingresada =");
        panel_output.add(label_operacion);

        label_resultado.setFont(new java.awt.Font("Georgia", 0, 18)); // NOI18N
        label_resultado.setForeground(new java.awt.Color(0, 153, 51));
        label_resultado.setText("jLabel4");
        panel_output.add(label_resultado);

        label_error.setFont(new java.awt.Font("Georgia", 2, 13)); // NOI18N
        label_error.setForeground(new java.awt.Color(153, 0, 0));
        label_error.setText("jLabel5");
        panel_output.add(label_error);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(sep_1))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(input_postfix)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btn_operar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(panel_output, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(label_descripcion)
                                .add(0, 0, Short.MAX_VALUE))))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(label_title)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(sep_2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(18, 18, 18)
                .add(label_title, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sep_1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btn_operar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(input_postfix, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(label_descripcion)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sep_2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel_output, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_operarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_operarActionPerformed
        try{
            String postfix = input_postfix.getText();
            
            double result = this.calculadora.operar(postfix);
            
            DecimalFormat df = new DecimalFormat();
            df.setMinimumFractionDigits(0);
            df.setMaximumFractionDigits(3);
            label_resultado.setText(df.format(result));
            label_operacion.setText("  "+postfix+" =  ");
            label_error.setVisible(false);
            label_resultado.setVisible(true);
            label_operacion.setVisible(true);
            
        }catch(Exception err){
            label_error.setText(err.getMessage());
            label_error.setVisible(true);
            label_resultado.setVisible(false);
            label_operacion.setVisible(false);
        }
    }//GEN-LAST:event_btn_operarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(calcupostfix_form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(calcupostfix_form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(calcupostfix_form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(calcupostfix_form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new calcupostfix_form().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_operar;
    private javax.swing.JTextField input_postfix;
    private javax.swing.JLabel label_descripcion;
    private javax.swing.JLabel label_error;
    private javax.swing.JLabel label_operacion;
    private javax.swing.JLabel label_resultado;
    private javax.swing.JLabel label_title;
    private javax.swing.JPanel panel_output;
    private javax.swing.JSeparator sep_1;
    private javax.swing.JSeparator sep_2;
    // End of variables declaration//GEN-END:variables
}
